#define  _CRT_SECURE_NO_WARNINGS 1
#include <cstring>
#include <cassert>
namespace lw
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator begin() const
		{
			return _str;
		}
		const_iterator end() const
		{
			return _str + _size;
		}
		string(const char* str="")
			:_str(nullptr)
			,_size(0)
			,_capacity(0)
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_size + 1];

			strcpy(_str, str);
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}
		string(const string& s)
			:_str(nullptr)
			, _size(0)
			, _capacity(0)
		{
			string tmp(s._str);
			swap(tmp);

		}

		string(string&& s)
			:_str(nullptr)
			, _size(0)
			, _capacity(0)
		{
			swap(s);
		}

		string& operator=(string s)
		{
			swap(s);
			return *this;
		}

		string& operator=(string&& s)
		{
			swap(s);
			return *this;
		}

		const char* c_str() const
		{
			return _str;
		}
		size_t size() const
		{
			return _size;
		}
		size_t capacity() const
		{
			return _capacity;
		}
		void swap(string& s)
		{
			::swap(_str, s._str);
			::swap(_size, s._size);
			::swap(_capacity, s._capacity);
		}
		char& operator[](size_t size)
		{
			assert(size < _size);

			return *(_str + size);
		}
		const char& operator[](size_t size) const
		{
			assert(size < _size);

			return *(_str + size);
		}
		void reserve(size_t n)
		{
			if (_capacity < n)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}
		void resize(size_t n, char ch = '\0')
		{
			if (size() < n)
			{
				reserve(n);
				for (size_t i =_size; i < n; i++)
				{
					_str[i] = ch;
				}
			}
			_str[n] = '\0';
			_size = n;
			
		}
		void push_back(char ch)
		{
			insert(_size, ch);
		}
		void append(const char* str)
		{
			insert(_size, str);
		}
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}
		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}
		string& insert(size_t pos, const char* str)
		{
			assert(pos <= _size);

			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			size_t end = _size + len;
			while (end >= pos + len)
			{
				_str[end] = _str[end - len ];
				end--;
			}
			strncpy(_str + pos, str, len);
			_size += len;
			return *this;
		}
		string& insert(size_t pos, char ch)
		{
			assert(pos <= _size);

			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}
			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				end--;
			}
			_str[pos] = ch;
			_size++;
			return *this;
		}
		bool empty() const
		{
			return size() == 0;
		}
		size_t find(char c, size_t pos = 0) const
		{
			assert(pos < _size);

			for (size_t i = pos; i < size(); ++i)
			{
				if (_str[i] == c)
					return i;
			}
			return npos;
		}
		string substr(size_t pos = 0, size_t len = npos) const
		{
			assert(pos < _size);

			string ret;
			len = len == npos ? size() - pos : len;
			ret.reserve(len);
			for (size_t i = pos; i < pos + len; ++i)
			{
				ret += _str[i];
			}
			ret._str[ret._size] = '\0';
			return ret;
		}
		const static size_t npos = -1;
		bool operator>(const string& s) const
		{
			return strcmp(_str, s._str) > 0;
		}
		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}
		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}
		bool operator>=(const string& s) const
		{
			return *this > s || *this == s;
		}
		bool operator<(const string& s) const
		{
			return !(*this >= s);
		}
		bool operator<=(const string& s) const
		{
			return !(*this > s);
		}
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	};
	ostream& operator<<(ostream& out, const string& s)
	{
		for (size_t i = 0; i < s.size(); ++i)
		{
			cout << s[i];
		}
		return out;
	}

	void TestString1()
	{
		string s("hello world");
		for (auto ch : s)
		{
			cout << ch << " ";
		}
		cout << endl;
	}
	void TestString2()
	{
		string s("hello world");
		for (int i = 0; i < s.size(); ++i)
		{
			cout << s[i] << " ";
		}
		cout << endl;
	}
	void TestString3()
	{
		string s("hello");
		s.insert(2, "the");
		cout << s << endl;
		s.insert(0, '!');
		cout << s;
	}
	void TestString4()
	{
		string s1("hello");
		string s2 = s1.substr(s1.find('e'), 2);
	}
	void TestString5()
	{
		string s("https://legacy.cplusplus.com/reference/string/string/substr/");
		size_t pos1 = s.find('/');
		if (pos1 == string::npos)
		{
			perror("pos1 == npos");
			exit(-1);
		}
		string domain = s.substr(0, pos1);
		size_t pos2 = s.find('/', pos1 + 2);
		if (pos2 == string::npos)
		{
			perror("pos2 == npos");
			exit(-1);
		}
		string cont = s.substr(pos1 + 2, pos2 - pos1 - 2);
		string rest = s.substr(pos2 + 1);

		cout << domain << endl;
		cout << cont << endl;
		cout << rest << endl;
	}
}